//
//  mainpage.m
//  firstllk
//
//  Created by Huang Michael on 5/24/10.
//  Copyright 2010 LotusVille. All rights reserved.
//
//	logs:
//	2010-6-27	Yangss	美化主菜单


#import "mainpage.h"
#import "GameControl.h"
#import "HelpLayer.h"
#import "HighScoresLayer.h"
#import "Record.h"

//static BOOL g_bSound = YES;	//音效开关全局变量

@implementation mainpage
+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	mainpage *layer = [mainpage node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	self = [super init];
	if (!self) {
		return nil;
	}
	
	//设置背景图
	CCSprite *spBg = [CCSprite spriteWithFile:@"mainBg.png"];
	spBg.anchorPoint = CGPointZero;
	[self addChild:spBg z:0 tag:1];
	
	// ask director the the window size
	CGSize size = [[CCDirector sharedDirector] winSize];
	
	
	//添加菜单项
	CCSprite *spNewgame = [CCSprite spriteWithFile:@"mainBgNewgame.png"];	
	CCMenuItem *miNewgame = [CCMenuItemSprite itemFromNormalSprite:spNewgame selectedSprite:spNewgame target:self selector:@selector(classicPlay:)];

	CCSprite *spRecord = [CCSprite spriteWithFile:@"mainBgRecord.png"];
	CCMenuItem *miRecord = [CCMenuItemSprite itemFromNormalSprite:spRecord selectedSprite:spRecord target:self selector:@selector(OnRecord:)];

	CCSprite *spHelp = [CCSprite spriteWithFile:@"mainBgHelp.png"];
	CCMenuItem *miHelp = [CCMenuItemSprite itemFromNormalSprite:spHelp selectedSprite:spHelp target:self selector:@selector(OnHelp:)];
	
	CCMenu *mMain = [CCMenu menuWithItems:miNewgame, miRecord, miHelp, nil];
	//设置菜单项对齐模式
	[mMain alignItemsVertically];
	
	//设置菜单颜色，位置，缩放比例
	//mn.color = ccc3(80,200,250);
	mMain.position = ccp( size.width /2 , 180 );
	//mn.scale = 1.3;
	
	[self addChild:mMain z:1 tag:2];
	
	//添加音乐开关
	CCSprite *spSoundOn = [CCSprite spriteWithFile:@"soundOn.png"];
	CCSprite *spSoundOff = [CCSprite spriteWithFile:@"soundOff.png"];
	CCMenuItemToggle *mitSound = [CCMenuItemToggle itemWithTarget:self selector:@selector(OnSetSound:) items:
								  [CCMenuItemSprite itemFromNormalSprite:spSoundOn selectedSprite:spSoundOn],
								  [CCMenuItemSprite itemFromNormalSprite:spSoundOff selectedSprite:spSoundOff],
								  nil];	
#ifdef EN_SOUND
	mitSound.selectedIndex = [SimpleAudioEngine sharedEngine].muted;	//使用当前音效状态选中音效开关
//	mitSound.selectedIndex = ([SimpleAudioEngine sharedEngine].muted == YES?0:1);	//使用当前音效状态选中音效开关
#endif

	CCMenu *mSound = [CCMenu menuWithItems:mitSound, nil];
	mSound.position = ccp(size.width-20, size.height-20);
	[self addChild:mSound z:2 tag:3];
	
	return self;
}

- (void) classicPlay:(id) sender
{
	//判断是否有存盘
	//ToDo:以后补充代码
	[self newGame];
	
}

- (void) newGame
{
	CCScene *sc = [CCScene node];
	GameControl * gcNode = [GameControl node];
	[sc addChild:gcNode];
//	[sc addChild:[GameCntrolMenu node]];
	
//	[[CCDirector sharedDirector] pushScene: [CCTurnOffTilesTransition transitionWithDuration:0.5f scene:sc]];	
	[[CCDirector sharedDirector] replaceScene: [CCTurnOffTilesTransition transitionWithDuration:0.5f scene:sc]];	
}

//进入设置
- (void) OnHelp:(id) sender
{
	CCScene *sc = [CCScene node];
	[sc addChild:[HelpLayer node]];
	
	[[CCDirector sharedDirector] replaceScene: [CCZoomFlipYTransition transitionWithDuration:0.5f scene:sc]];
}

//设置声音
- (void) OnSetSound:(id) sender
{
#ifdef EN_SOUND
	[SimpleAudioEngine sharedEngine].muted = [sender selectedIndex];//==0?YES:NO;
#endif	
}

//进入排行榜
- (void) OnRecord:(id) sender
{
	//test code: 随机增加一个排行榜分数
//	for (int i=0; i<1; i++) {
//		[User saveName:@"C"];
//		[User saveScore:33];
//		[Record trySaveScoreRecord:[Record createCurrentRecord]];
//
//	[User saveName:@"qretr"];
//	[User saveScore:103919];
//	[Record trySaveScoreRecord:[Record createCurrentRecord]];
//	
//	[User saveName:@""];
//	[User saveScore:5455];
//	[Record trySaveScoreRecord:[Record createCurrentRecord]];
//	
//	[User saveName:@"中文"];
//	[User saveScore:12345];
//	[Record trySaveScoreRecord:[Record createCurrentRecord]];
	//	}
	//test code end
	
	[Record getHighScores];
	
	CCScene *sc = [CCScene node];
	[sc addChild:[HighScoresLayer node]];
	[[CCDirector sharedDirector] replaceScene: [CCZoomFlipXTransition transitionWithDuration:0.5f scene:sc]];
	
	
	//CCFadeTRTransition 分块翻
}

-(void) onQuit: (id) sender
{
	/*
	 [[CCDirector sharedDirector] end];
	 
	 // HA HA... no more terminate on sdk v3.0
	 // http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
	 if( [[UIApplication sharedApplication] respondsToSelector:@selector(terminate)] )
	 [[UIApplication sharedApplication] performSelector:@selector(terminate)];
	 else
	 CCLOG(@"YOU CAN'T TERMINATE YOUR APPLICATION PROGRAMATICALLY in SDK 3.0+");
	 */
}



// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
